﻿using AssettDatabase.VO;
using System;
using System.Collections.Generic;
using System.Data;

namespace AssettDatabase.BLL
{
    class Asset
    {
        public long Add(int Customer_ID, string Serial_Number, string Model_Number, int Asset_Type_ID,
                        string Vendor_Name, DateTime A_S_Purchase_Date, DateTime Sold_To_Customer_Date, 
                        string A_S_Invoice_Number, int Supplier_ID, DateTime Software_Renewal_Date, DateTime Warranty_Expiry, 
                        string Supplier_Invoice_Number, string Asset_Notes, List<VO.Asset_Image> Asset_Images, int Asset_InStock)
        {
            DAL.Asset mAsset = new DAL.Asset();
            return mAsset.Add(Customer_ID, Serial_Number,  Model_Number, Asset_Type_ID, Vendor_Name, A_S_Purchase_Date,
                            Sold_To_Customer_Date, A_S_Invoice_Number, Supplier_ID, Software_Renewal_Date, Warranty_Expiry, Supplier_Invoice_Number, Asset_Notes, Asset_Images, Asset_InStock);
        }

        public bool Update(int Customer_ID, string Serial_Number, string Model_Number, int Asset_Type_ID,
                string Vendor_Name, DateTime A_S_Purchase_Date, DateTime Sold_To_Customer_Date,
                string A_S_Invoice_Number, int Supplier_ID, DateTime Software_Renewal_Date, DateTime Warranty_Expiry,
                string Supplier_Invoice_Number, string Asset_Notes, List<VO.Asset_Image> Asset_Images, int Asset_InStock, int Asset_ID)
        {
            DAL.Asset mAsset = new DAL.Asset();
            return mAsset.Update(Customer_ID, Serial_Number, Model_Number, Asset_Type_ID, Vendor_Name, A_S_Purchase_Date,
                            Sold_To_Customer_Date, A_S_Invoice_Number, Supplier_ID, Software_Renewal_Date, Warranty_Expiry, Supplier_Invoice_Number, Asset_Notes, Asset_Images, Asset_InStock, Asset_ID);
        }

        public bool Del(int Asset_ID)
        {
            DAL.Asset mAsset = new DAL.Asset();
            return mAsset.Del(Asset_ID);
        }

        public bool Del_Asset_Image(int Asset_ID, string FileName)
        {
            DAL.Asset mAsset = new DAL.Asset();
            return mAsset.Del_Asset_Image(Asset_ID, FileName);
        }

        public List<VO.Asset> GetAsset()
        {
            List<VO.Asset> mList = new List<VO.Asset>();
            DAL.Asset mAsset = new DAL.Asset();
            DataTable mDataTable = mAsset.GetAsset();

            foreach (DataRow dr in mDataTable.Rows)
            {
                VO.Asset voAsset = new VO.Asset();
                voAsset.A_S_Invoice_Number = dr["A_S_Invoice_Number"].ToString();
                voAsset.A_S_Purchase_Date = Convert.ToDateTime(dr["A_S_Purchase_Date"].ToString());
                voAsset.Asset_ID = Convert.ToInt32(dr["Asset_ID"].ToString());
                voAsset.Asset_Notes = System.Text.Encoding.Default.GetString((byte[])dr["Asset_Notes"]);
                voAsset.Asset_Type_ID = Convert.ToInt32(dr["Asset_Type_ID"].ToString());
                voAsset.Customer_ID = Convert.ToInt32(dr["Customer_ID"].ToString());
                voAsset.Model_Number = dr["Model_Number"].ToString();
                voAsset.Serial_Number = dr["Serial_Number"].ToString();
                voAsset.Software_Renewal_Date = Convert.ToDateTime(dr["Software_Renewal_Date"].ToString());
                voAsset.Sold_To_Customer_Date = Convert.ToDateTime(dr["Sold_To_Customer_Date"].ToString());
                voAsset.Supplier_ID = Convert.ToInt32(dr["Supplier_ID"].ToString());
                voAsset.Supplier_Invoice_Number = dr["Supplier_Invoice_Number"].ToString();
                voAsset.Warranty_Expiry = Convert.ToDateTime(dr["Warranty_Expiry"].ToString());
                voAsset.Customer_Name = dr["Customer_Name"].ToString();
                voAsset.Asset_InStock = Convert.ToInt32(dr["Asset_InStock"].ToString());

                mList.Add(voAsset);
            }

            return mList;
        }
        public List<VO.Asset> GetAssetById(int Asset_ID)
        {
            List<VO.Asset> mList = new List<VO.Asset>();
            List<VO.Asset_Image> mListImages = new List<Asset_Image>();

            DAL.Asset mAsset = new DAL.Asset();
            DataTable mDataTable = mAsset.GetAssetById(Asset_ID);
            DataTable mDataTable_Images = mAsset.GetAsset_ImagesById(Asset_ID);

            foreach (DataRow dr in mDataTable.Rows)
            {
                VO.Asset voAsset = new VO.Asset();
                voAsset.A_S_Invoice_Number = dr["A_S_Invoice_Number"].ToString();
                voAsset.A_S_Purchase_Date = Convert.ToDateTime(dr["A_S_Purchase_Date"].ToString());
                voAsset.Asset_ID = Convert.ToInt32(dr["Asset_ID"].ToString());
                voAsset.Asset_Notes = System.Text.Encoding.Default.GetString((byte[])dr["Asset_Notes"]);
                voAsset.Asset_Type_ID = Convert.ToInt32(dr["Asset_Type_ID"].ToString());
                voAsset.Customer_ID = Convert.ToInt32(dr["Customer_ID"].ToString());
                voAsset.Model_Number = dr["Model_Number"].ToString();
                voAsset.Serial_Number = dr["Serial_Number"].ToString();
                voAsset.Software_Renewal_Date = Convert.ToDateTime(dr["Software_Renewal_Date"].ToString());
                voAsset.Sold_To_Customer_Date = Convert.ToDateTime(dr["Sold_To_Customer_Date"].ToString());
                voAsset.Supplier_ID = Convert.ToInt32(dr["Supplier_ID"].ToString());
                voAsset.Supplier_Invoice_Number = dr["Supplier_Invoice_Number"].ToString();
                voAsset.Warranty_Expiry = Convert.ToDateTime(dr["Warranty_Expiry"].ToString());
                voAsset.Vendor_Name = dr["Vendor_Name"].ToString();
                voAsset.Customer_Name = dr["Customer_Name"].ToString();
                voAsset.Asset_InStock = Convert.ToInt32(dr["Asset_InStock"].ToString());

                foreach (DataRow dr_img in mDataTable_Images.Rows)
                {
                    VO.Asset_Image voAsset_Image = new VO.Asset_Image();

                    voAsset_Image.Asset_ID = Convert.ToInt32(dr_img["Asset_ID"].ToString());
                    voAsset_Image.FileName = dr_img["FileName"].ToString();
                    voAsset_Image.Image_ID = Convert.ToInt32(dr_img["Image_ID"].ToString());
                    mListImages.Add(voAsset_Image);
                }

                voAsset.Asset_Image = mListImages;
                mList.Add(voAsset);
            }

            return mList;
        }

        public DataTable GetAssetsData(int Customer_ID, string Serial_Number, string Model_Number, int Asset_Type_ID,
                        string Vendor_Name, int Supplier_ID, DateTime Warranty_From, DateTime Warranty_To,
                        bool Expired)
        {
            DAL.Asset mAsset = new DAL.Asset();
            DataTable mDataTable = mAsset.GetAssetsData(Customer_ID, Serial_Number, Model_Number, Asset_Type_ID,
                                                        Vendor_Name, Supplier_ID, Warranty_From, Warranty_To, Expired);
            return mDataTable;
        }

    }
}
